package gb.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gb.demo.common.R;
import gb.demo.entity.Movielabel;
import gb.demo.entity.User;
import gb.demo.service.MovieLabelService;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Controller
@MapperScan("gb.demo.mapper")
public class MovieLabelController {
    @Autowired
    private MovieLabelService movieLabelService;

/*    @RequestMapping("/label/allLabel")
    @ResponseBody
    public R labelTest() {
        List<Movielabel> list = movieLabelService.list();
        list.stream().forEach(System.out::println);
        return R.success(list);
    }*/
//    删除特定电影标签
    @RequestMapping("/movie/label/delete")
    @ResponseBody
    public R MovieLabelDelete(Movielabel ml, HttpSession session){
        User user=(User)session.getAttribute("loginUser");
        if(user != null && user.getIsadmin() == 1){
            QueryWrapper<Movielabel> movielabelQueryWrapper=new QueryWrapper<>();
            movielabelQueryWrapper.eq("movieid",ml.getMovieid()).eq("labelid",ml.getLabelid());
            if(movieLabelService.remove(movielabelQueryWrapper)){
                return R.success("删除成功");
            }
            else return R.error("删除失败");
        }
        else  return R.error("请登录管理员账号重试");
    }
//  添加标签
    @RequestMapping("/movie/label/insert")
    @ResponseBody
    public R MOvieLabelInsert(Movielabel ml, HttpSession session){
        User user=(User)session.getAttribute("loginUser");
        if(user != null && user.getIsadmin() == 1){
            QueryWrapper<Movielabel> movielabelQueryWrapper=new QueryWrapper<>();
            movielabelQueryWrapper.eq("movieid",ml.getMovieid()).eq("labelid",ml.getLabelid());
            Movielabel l=movieLabelService.getOne(movielabelQueryWrapper);
            if(l!=null)
                return R.error("该已存在该标签");
            if(movieLabelService.save(ml))
                    return R.success("添加成功");
            else return R.error("添加失败");
        }
        else  return R.error("请登录管理员账号重试");
    }
}
